//
//  ProgressView.m
//  Untitled
//
//  Created by  user on 11-4-29.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "ProgressView.h"

#import <QuartzCore/QuartzCore.h>

CGFloat const ProgressViewLabelLeftSpace = 40;

CGFloat const ProgressViewActiviSize = 20;


@interface CProgressView :
//#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
//UIVisualEffectView
//#else
UIView
//#endif
{
    UIActivityIndicatorView* _activity;
    UILabel* _title;
    UILabel* _text;
    UIImageView *_blurImageView;
}

@property(nonatomic,retain) UIActivityIndicatorView* activity;
@property(nonatomic,retain) UILabel* title;
@property(nonatomic,retain) UILabel* text;
@property(nonatomic,retain) UIImageView *blurImageView;

@end

@implementation CProgressView 

@synthesize activity = _activity;
@synthesize title = _title;
@synthesize text = _text;
@synthesize blurImageView = _blurImageView;

-(void)dealloc
{
    [_blurImageView release_mm];
	[_title release_mm];
	[_text release_mm];
	[_activity release_mm];
	superdealloc_mm;
}

@end

@implementation CProgressViewPanel

@synthesize progressView = _progressView;
@synthesize nCount = _nCount;
@synthesize delegate = _delegate;

-(void)dealloc
{
    self.delegate=nil;
	_progressView=nil;
	superdealloc_mm;
}

//-(void)setM_delegate:(id<CProgressViewPanelDelegate>)delegate
//{
//	if(nil==m_delegate)
//		m_delegate=delegate;
//	else if(m_delegate!=delegate)
//	{
//		[m_delegate PregressViewPanelDelegateDidExchange];
//		m_delegate=delegate;
//	}
//}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	if(_progressView!=nil)
	{
		UITouch* t=[((NSSet*)[event touchesForView:_progressView]) anyObject];
		if(t!=nil)
			[_delegate touchProgressViewPanel:self];
	}
}

@end

@implementation CProgressViewPanelHelper

static BOOL s_bBlurBackground = NO;

@synthesize pvpanel = _pvpanel;
@synthesize delayReleaseTime = _delayReleaseTime;

-(id)init
{
	self=[super init];
	_pvplock=[NSLock new];
	return self;
}

-(void)dealloc
{
	[_pvplock release_mm];
	superdealloc_mm;
}

-(void)progressViewPanelHelperWithActivity:(BOOL)bActivity title:(NSString *)szTitle text:(NSString *)szText delegate:(id<CProgressViewPanelDelegate>)delegate
{	
    void(^fun)()=^
    {
        CProgressView* progressview=nil;
        CGRect frame=CGRectZero;
        if(nil==_pvpanel)
        {
            _pvpanel=[[CProgressViewPanel alloc]initWithFrame:frame];
            frame=[delegate getProgressViewPanelCoverRect:_pvpanel];
            _pvpanel.frame=frame;
            _pvpanel.delegate=delegate;
            UIView* view=[delegate getProgressViewPanelParent:_pvpanel];
            [view addSubview:_pvpanel];
            _pvpanel.backgroundColor=[UIColor clearColor];
            _pvpanel.nCount = 1;
//#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
//            if([UIDevice isIOS8OrNewer])
//                progressview=[[CProgressView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
//            else
//            {
//                progressview=[[CProgressView alloc]initWithFrame:CGRectZero];
//                progressview.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.9];
//            }
//#else
            progressview=[[CProgressView alloc]initWithFrame:CGRectZero];
            progressview.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.9];
//#endif
            progressview.clipsToBounds = YES;
            progressview.layer.cornerRadius=10;
            _pvpanel.progressView=progressview;
            [_pvpanel addSubview:progressview];
            [progressview release_mm];
        }
        else
        {
            _pvpanel.nCount+=1;
            progressview=_pvpanel.progressView;
            frame=[_pvpanel.delegate getProgressViewPanelCoverRect:_pvpanel];
        }
        
        if(YES==bActivity)
        {
            UIActivityIndicatorView* activity=progressview.activity;
            if(nil==activity)
            {
                activity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
                [activity startAnimating];
                [progressview addSubview:activity];
                progressview.activity=activity;
                [activity release_mm];
            }
        }
        else
        {
            [progressview.activity removeFromSuperview];
         //   [progressview.m_activity release];
            progressview.activity=nil;
        }
        if(szTitle.length>0)
        {
            UILabel* label=progressview.title;
            if(nil==label)
            {
                label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
                label.font=[UIFont systemFontOfSize:17];
                label.textColor=[UIColor whiteColor];
                label.textAlignment=NSTextAlignmentCenter;
                label.backgroundColor=[UIColor clearColor];
                [progressview addSubview:label];
                progressview.title=label;
                [label release_mm];
            }
            label.text=szTitle;
        }
        else
        {
            [progressview.title removeFromSuperview];
        //    [progressview.m_title release];
            progressview.title=nil;
        }
        if(szText.length>0)
        {
            UILabel* label=progressview.text;
            if(nil==label)
            {
                label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
                label.font=[UIFont systemFontOfSize:15];
                label.textAlignment=NSTextAlignmentCenter;
                label.lineBreakMode=NSLineBreakByWordWrapping;
                label.numberOfLines=0;
                label.textColor=[UIColor whiteColor];
                label.backgroundColor=[UIColor clearColor];
                [progressview addSubview:label];
                progressview.text=label;
                [label release_mm];
            }
            label.text=szText;
        }
        else
        {
            [progressview.text removeFromSuperview];
        //    [progressview.m_text release];
            progressview.text=nil;
        }
        
        if(YES==bActivity&&szTitle.length>0)
        {
            if(szText.length<=0)
            {
                progressview.activity.frame=CGRectMake(10,10,ProgressViewActiviSize,ProgressViewActiviSize);
                CGSize size=[szTitle stringSizeWithFont:progressview.title.font forWidth:(CGRectGetWidth(frame)-40)-(ProgressViewActiviSize+10)];
                progressview.title.frame=CGRectMake(ProgressViewLabelLeftSpace,10,size.width,size.height);
                progressview.frame=CGRectMake(0,0,size.width+ProgressViewLabelLeftSpace+10,size.height+20);
            }
            else 
            {
                //与settext中的代码相似
                CGSize size1=[szTitle stringSizeWithFont:progressview.title.font forWidth:CGRectGetWidth(frame)-(ProgressViewActiviSize+10)];
                CGSize size2=[szText wrapStringSizeWithFont:progressview.text.font forConstrainSize:CGSizeMake((CGRectGetWidth(frame)-40)-(ProgressViewActiviSize+10),280)];
                CGFloat n=MAX(size1.width,size2.width);
                progressview.title.frame=CGRectMake(ProgressViewLabelLeftSpace,10,n,size1.height);
                progressview.text.frame=CGRectMake(ProgressViewLabelLeftSpace,10+size1.height,n,size2.height+5);
                progressview.activity.frame=CGRectMake(10,10+(size1.height+size2.height+5-ProgressViewActiviSize)/2,ProgressViewActiviSize,ProgressViewActiviSize);
                progressview.frame=CGRectMake(0,0,n+ProgressViewLabelLeftSpace+10,size1.height+size2.height+ProgressViewActiviSize+5);
            }
        }
        else if(YES==bActivity&&szTitle.length<=0)
        {
            if(szText.length<=0)
            {
                progressview.activity.frame=CGRectMake(10,10,ProgressViewActiviSize,ProgressViewActiviSize);
                progressview.frame=CGRectMake(0,0,progressview.activity.width+20,progressview.activity.height+20);
            }
        }
        else if(NO==bActivity&&szTitle.length>0)
        {
            if(szText.length<=0)
            {
                CGSize size=[szTitle stringSizeWithFont:progressview.title.font forWidth:(CGRectGetWidth(frame)-40)];
                progressview.title.frame=CGRectMake(10,10,size.width,size.height);
                progressview.frame=CGRectMake(0,0,size.width+10+10,size.height+20);
            }
            else
            {
                CGSize size1=[szTitle stringSizeWithFont:progressview.title.font forWidth:CGRectGetWidth(frame)-40];
                CGSize size2=[szText wrapStringSizeWithFont:progressview.text.font forConstrainSize:CGSizeMake((CGRectGetWidth(frame)-40),280)];
                CGFloat n=MAX(size1.width,size2.width);
                progressview.title.frame=CGRectMake(10,10,n,size1.height);
                progressview.text.frame=CGRectMake(10,10+size1.height,n,size2.height+5);
                progressview.frame=CGRectMake(0,0,n+10+10,size1.height+size2.height+20);
            }
        }
        else if(NO==bActivity&&szTitle.length<=0)
        {
            if(szText.length>0)
            {
                CGSize size=[szText wrapStringSizeWithFont:progressview.text.font forConstrainSize:CGSizeMake((CGRectGetWidth(frame)-40),280)];
                progressview.text.frame=CGRectMake(10,5,size.width,size.height);
                progressview.frame=CGRectMake(0,0,size.width+10+10,size.height+10);
            }
        }
        
   //     progressview.center=CGPointMake(_pvpanel.frame.size.width/2,_pvpanel.frame.size.height/2);
        progressview.center = [_pvpanel.delegate getProgressViewCenterPosition:_pvpanel];

        if(1==_pvpanel.nCount)
        {	
            [UIView animateWithDuration:0.3 animations:^
            {
                _pvpanel.alpha=1.0;
            }];
        }
    };
    DispatchMainThread(NO, ^
    {
        fun();
    });
}

-(void)retainProgressViewPanel
{
    DispatchMainThread(NO, ^
    {
        if(_pvpanel!=nil&&_pvpanel.nCount>=1)
            _pvpanel.nCount++;
    });
}

-(void)releaseProgressViewPanelOnMainThread
{
    DispatchMainThread(NO, ^
    {
        if(_pvpanel!=nil)
        {
            NSInteger n=_pvpanel.nCount;
            _pvpanel.nCount-=1;
            if(1==n)
            {
                CProgressViewPanel* pvpanel=[[_pvpanel retain_mm]autorelease_mm];
                _pvpanel=nil;
                [UIView animateWithDuration:0.3 animations:^
                 {
                     pvpanel.alpha=0.0;
                 }completion:^(BOOL finished)
                 {
                     [pvpanel removeFromSuperview];
                     [pvpanel.delegate progressViewPanelDidRemove:pvpanel];
                     pvpanel.delegate=nil;
                     [pvpanel release_mm];
                 }];
            }
        }
    });
}

-(void)releaseProgressViewPanel
{
	if(_delayReleaseTime>0.0)
    {
        dispatch_async(dispatch_get_main_queue(),^
        {
            [self performSelector:@selector(releaseProgressViewPanelOnMainThread) withObject:nil afterDelay:_delayReleaseTime];
        });
    }
	else
		[self releaseProgressViewPanelOnMainThread];
}

-(void)forceReleaseProgressViewPanel
{
    DispatchMainThread(NO, ^
    {
        if(_pvpanel.nCount>0)
        {
            [CProgressViewPanelHelper cancelPreviousPerformRequestsWithTarget:self selector:@selector(ReleaseProgressViewPanelOnMainThread) object:nil];
            _pvpanel.nCount=1;
            [self releaseProgressViewPanelOnMainThread];
        }
    });
}

-(void)setText:(NSString *)szText
{
    DispatchMainThread(NO, ^
    {
        if(nil==_pvpanel.progressView.text)
        {
            NSString* szTitle=_pvpanel.progressView.title.text;
            UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
            label.font=[UIFont systemFontOfSize:15];
            label.textAlignment=NSTextAlignmentCenter;
            label.lineBreakMode=NSLineBreakByWordWrapping;
            label.numberOfLines=0;
            label.textColor=[UIColor whiteColor];
            label.backgroundColor=[UIColor clearColor];
            label.text=szText;
            [_pvpanel.progressView addSubview:label];
            _pvpanel.progressView.text=label;
            [label release_mm];
            
            
            CGSize size1=[szTitle stringSizeWithFont:_pvpanel.progressView.title.font forWidth:280];
            CGSize size2=[szText wrapStringSizeWithFont:_pvpanel.progressView.text.font forConstrainSize:CGSizeMake(280,280)];
            CGFloat n=MAX(size1.width,size2.width);
            _pvpanel.progressView.title.frame=CGRectMake(ProgressViewLabelLeftSpace,10,n,size1.height);
            _pvpanel.progressView.text.frame=CGRectMake(ProgressViewLabelLeftSpace,10+size1.height,n,size2.height+5);
            _pvpanel.progressView.activity.frame=CGRectMake(10,10+(size1.height+size2.height+5-ProgressViewActiviSize)/2,ProgressViewActiviSize,ProgressViewActiviSize);
            _pvpanel.progressView.frame=CGRectMake(0,0,n+ProgressViewLabelLeftSpace+10,size1.height+size2.height+ProgressViewActiviSize+5);
        //    _pvpanel.progressView.center=CGPointMake(_pvpanel.frame.size.width/2,_pvpanel.frame.size.height/2);
        }
        else
        {
            NSString* text=_pvpanel.progressView.text.text;
            CGSize size3=[text wrapStringSizeWithFont:_pvpanel.progressView.text.font forConstrainSize:CGSizeMake(280,280)];
            CGSize size2=[szText wrapStringSizeWithFont:_pvpanel.progressView.text.font forConstrainSize:CGSizeMake(280,280)];
            if(ABS(size3.width-size2.width)>5)
            {
                NSString* szTitle=_pvpanel.progressView.title.text;
                CGSize size1=[szTitle stringSizeWithFont:_pvpanel.progressView.title.font forWidth:280];
                CGFloat n=MAX(size1.width,size2.width);
                _pvpanel.progressView.title.frame=CGRectMake(ProgressViewLabelLeftSpace,10,n,size1.height);
                _pvpanel.progressView.text.frame=CGRectMake(ProgressViewLabelLeftSpace,10+size1.height,n,size2.height+5);
                _pvpanel.progressView.activity.frame=CGRectMake(10,10+(size1.height+size2.height+5-ProgressViewActiviSize)/2,ProgressViewActiviSize,ProgressViewActiviSize);
                _pvpanel.progressView.frame=CGRectMake(0,0,n+ProgressViewLabelLeftSpace+10,size1.height+size2.height+ProgressViewActiviSize+5);
            //    _pvpanel.progressView.center=CGPointMake(_pvpanel.frame.size.width/2,_pvpanel.frame.size.height/2);
            }
            _pvpanel.progressView.text.text=szText;
        }
        
       // _pvpanel.progressView.center=CGPointMake(_pvpanel.frame.size.width/2,_pvpanel.frame.size.height/2);
        
        _pvpanel.progressView.center = [_pvpanel.delegate getProgressViewCenterPosition:_pvpanel];
    });
}

+(void)setBlurBackground:(BOOL)blurBackground
{
    s_bBlurBackground = blurBackground;
}

@end
